package com.yy.yyui.combobox
{
	import com.siku.manor.common.EmptyFunction;
	import com.siku.manor.view.common.GrayBut;
	import com.siku.manor.view.common.HCMdownBut;
	import com.siku.manor.view.common.HCMupBut;
	import com.siku.manor.view.common.UM;
	import com.siku.manor.view.component.text.TextLabel;
	
	import flash.display.DisplayObject;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextFormatAlign;
	
	public class UpDownCombox extends Sprite
	{
		private var upBut:SimpleButton;
		private var downBut:SimpleButton;
		private var _defaultNum:int;
		private var _maxNum:int;
		private var texts:TextLabel;
		private var _changeFunc:Function;
		public function UpDownCombox(defaultNum:int,maxNum:int,changeFunc:Function = null,bg:DisplayObject = null, preBtn:SimpleButton=null, nextBtn:SimpleButton=null,txtColor:uint=0xffffff)
		{
			super();
			this.changeFunc = changeFunc;
			this.addEventListener(Event.REMOVED_FROM_STAGE,udRemoveEvent);
			var dobj:DisplayObject;
			if(bg) {
				dobj = bg;
			}else {
				dobj = UM.scale9(new GrayBut());
			}
			dobj.width = 87;
			dobj.height = 24;
			var backG:DisplayObject = this.addChild(dobj);
			
			texts = new TextLabel("1 / "+maxNum,60);
			texts.textFormat.align = TextFormatAlign.CENTER;
			texts.textFormat.color = txtColor; 
			dobj = this.addChild(texts);
			dobj.x = (this.width -texts.width)/2;
			dobj.y = (this.height - texts.height)/2;
			
			if(preBtn) {
				this.upBut = preBtn;
			}else {
				this.upBut = UM.createFiltersImageButton(new HCMupBut());
			}
			if(nextBtn) {
				this.downBut = nextBtn;
			}else{
				this.downBut = UM.createFiltersImageButton(new HCMdownBut());
			}
			dobj = this.addChild(upBut);
			dobj.x = 4;
			dobj.y = (this.height - upBut.height)/2;
			
			dobj = this.addChild(downBut);
			dobj.x = backG.width - downBut.width-4;
			dobj.y = (this.height - upBut.height)/2;
			upBut.addEventListener(MouseEvent.CLICK,udUpEvent);
			downBut.addEventListener(MouseEvent.CLICK,udDownEvent);
			this.defaultNum = defaultNum;
			this.maxNum = maxNum;
		}
		private function udUpEvent(e:MouseEvent):void
		{
			if(defaultNum > 1)
			{
				defaultNum--;
			}
			this.runFunc();
			this.update();
		}
		private function udDownEvent(e:MouseEvent):void
		{
			if(defaultNum < maxNum)
			{
				defaultNum++;
			}
			this.runFunc();
			this.update();
		}
		
		private function udRemoveEvent(e:Event):void
		{
			upBut.removeEventListener(MouseEvent.CLICK,udUpEvent);
			downBut.removeEventListener(MouseEvent.CLICK,udUpEvent);
			this.changeFunc = null;
		}
		public function set maxNum(param:int):void
		{
			this._maxNum = param;
			this.update();
		}
		public function get maxNum():int
		{
			return this._maxNum;
		}
		public function set defaultNum(param:int):void
		{
			this._defaultNum = param;
			this.update();
		}
		public function get defaultNum():int
		{
			return this._defaultNum;
		}
		public function set changeFunc(param:Function):void
		{
			this._changeFunc = param;
		}
		public function get changeFunc():Function
		{
			return this._changeFunc;
		}
		public function update():void
		{
			texts.text = defaultNum+" / "+maxNum;
			if(this.maxNum == 0)
			{
				texts.text = "1 / 1";
			}
		}
		private function runFunc():void
		{
			if(this.changeFunc != null)
			{
				this.changeFunc(this.defaultNum);
			}
		}
	}
}
